//
//  TripAnnotionView.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/25.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import MapKit

enum AnnType {
    
    case enent
    case record
}

class TripAnnotionView: MKAnnotationView {
    
    private var evetnBGImageView:UIImageView!
    private var eventImageView:UIImageView!
    private var eventNameLabel:UILabel?
    
    private var recordBtn:UIButton?
    
    private var annType:AnnType
    
    var recordAction:((TripAnnotion?)->Void)?
    
    init(annotation: MKAnnotation?, reuseIdentifier: String?, anntype:AnnType) {
        
        annType = anntype
        
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {

        evetnBGImageView = UIImageView(image: UIImage(named: "annotation"))
        self.addSubview(evetnBGImageView)
        
        evetnBGImageView.snp.makeConstraints { maker in
            
            maker.centerX.equalToSuperview()
            maker.centerY.equalToSuperview().offset(-24)
            maker.width.equalTo(40)
            maker.height.equalTo(48)
        }
        
        eventImageView = UIImageView(image:  UIImage(named:"placeholder_squ"))
        eventImageView.layer.cornerRadius = 19
        eventImageView.clipsToBounds = true
        eventImageView.contentMode = .scaleAspectFill
        
        evetnBGImageView.addSubview(eventImageView)
        
        eventImageView.snp.makeConstraints { maker in
            
            maker.top.equalToSuperview().offset(1)
            maker.centerX.equalToSuperview()
            maker.width.equalTo(38)
            maker.height.equalTo(38)
        }
        
        if annType == .enent {
            
            setupEventLabel()
        } else {
            
            setupRecordBtn()
            evetnBGImageView.isHidden = true
            self.canShowCallout = false
        }
    }
    
    private func setupEventLabel() {
        
        let label = UILabel()
        
        label.font = kFont10
        label.textColor = .white
        label.backgroundColor = UIColor(white: 0, alpha: 0.6)
        label.layer.cornerRadius = 8
        label.textAlignment = .center
        label.clipsToBounds = true
        label.sizeToFit()
        self.addSubview(label)
        
        label.snp.makeConstraints {
            [weak self] maker in
            guard let `self` = self else { return }
            maker.top.equalTo(self.evetnBGImageView.snp.bottom).offset(5);
            maker.centerX.equalToSuperview();
            maker.height.equalTo(16);
            maker.width.equalTo(20);
        }
        
        self.eventNameLabel = label
    }
    
    private func setupRecordBtn() {
        
        let btn = UIButton(type: .custom)
        btn.backgroundColor = UIColor.hexStringToColor(hexString: "#00B8E4")
        btn.layer.cornerRadius = 14
        btn.layer.borderWidth = 2.0
        btn.layer.borderColor = UIColor.white.cgColor
        
        btn.addTarget(self, action: #selector(recoedTappedAction), for: .touchUpInside)
        
        self.addSubview(btn)
        
        btn.snp.makeConstraints { maker in
            
            maker.centerX.equalToSuperview()
            maker.centerY.equalToSuperview()
            maker.width.equalTo(28)
            maker.height.equalTo(28)
        }
        
        self.recordBtn = btn
    }
    
    @objc func recoedTappedAction() {
        
        recordAction?(self.annotation as? TripAnnotion)
    }
    
    func showRecordView() {
        
        self.evetnBGImageView.isHidden = false
        self.recordBtn!.isHidden = true
    }
    
    func hiddenRecordView() {
        
        self.evetnBGImageView.isHidden = true
        self.recordBtn!.isHidden = false
    }
    
    func setup(ann:TripAnnotion?) {
        
        self.annotation = ann
        
        guard let ann = ann else { return }
        
        
        if let model = ann.model as? Calendar {
            
            eventImageView.setImage(string: model.pictureArray?.first, placeholder: UIImage(named:"placeholder_squ")!)
            eventNameLabel?.text = model.title
            
            let width = (model.title?.widthForComment(fontSize: 10) ?? -16) + 16
            
            eventNameLabel?.snp.remakeConstraints {
                [weak self] maker in
                guard let `self` = self else { return }
                maker.top.equalTo(self.evetnBGImageView.snp.bottom).offset(5);
                maker.centerX.equalToSuperview();
                maker.height.equalTo(16);
                maker.width.equalTo(width);
            }
        }
        
        if let model = ann.model as? Record {
            
            eventImageView.setImage(string: model.pictureArray?.first, placeholder: UIImage(named:"placeholder_squ")!)
            recordBtn?.setTitle("\(model.number ?? 0)", for: .normal)
        }
    }
    
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {

        if self.bounds.contains(point) {

            return true
        }

        for view in self.subviews {
            
            if view.frame.contains(point) {
                
                return true
            }
        }
        
        return false
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if let view = super.hitTest(point, with: event) {
            
            self.bringSubviewToFront(view)
            return view
        }
        
        return nil
    }
    
}

